﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using DunLibrary.Dun;
using DunSilverlight.Comm;

namespace DunSilverlight.Views
{
    public partial class CommentForm : UserControl
    {
        public CaseComment Comment { get; set; }

        public Action SaveCallBack;

        public CommentForm(CaseComment comment)
        {
            Comment = comment;
            InitializeComponent();
            Loaded += new RoutedEventHandler(CommentForm_Loaded);
        }

        void CommentForm_Loaded(object sender, RoutedEventArgs e)
        {
            SetRight();
            DataContext = Comment;
        }

        private void SetRight()
        {
            bool right = RightManagement.VerifyRight(RightManagement.Right_Comment);

            btnSave.IsEnabled = right;
            txtComment.IsEnabled = right;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            CaseCommentService.CaseCommentServiceClient ser = new CaseCommentService.CaseCommentServiceClient();

            ser.SaveOrUpdateCompleted += (object sender1, CaseCommentService.SaveOrUpdateCompletedEventArgs e1) =>
                {
                    int id = JsonSerializerHelper.JsonToEntity<int>(e1.Result);
                    Comment.Id=id;
                    Tools.ShowMessage("保存成功!", "", true);
                    SaveCallBack();
                };

            string json=Newtonsoft.Json.JsonConvert.SerializeObject(Comment);
            ser.SaveOrUpdateAsync(json);
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }
    }
}
